<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php



class LoggerLoggingEvent {

	private static $startTime;

	
	private $fqcn;
	
	
	private $logger = null;
	
	
	private $categoryName;
	
	
	protected $level;
	
	
	private $ndc;
	
	
	private $ndcLookupRequired = true;
	
	
	private $mdcCopyLookupRequired = true;
	
	
	private $message;
	
	
	private $renderedMessage = null;
	
	
	private $threadName = null;
	
	
	public $timeStamp;
	
	
	private $locationInfo = null;
	
	
	private $throwableInfo = null;
	
	
	public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, Exception $throwable = null) {
		$this->fqcn = $fqcn;
		if($logger instanceof Logger) {
			$this->logger = $logger;
			$this->categoryName = $logger->getName();
		} else {
			$this->categoryName = strval($logger);
		}
		$this->level = $priority;
		$this->message = $message;
		if($timeStamp !== null && is_float($timeStamp)) {
			$this->timeStamp = $timeStamp;
		} else {
			if(function_exists('microtime')) {
								$this->timeStamp = microtime(true);
			} else {
				$this->timeStamp = floatval(time());
			}
		}
		
		if ($throwable !== null && $throwable instanceof Exception) {
			$this->throwableInfo = new LoggerThrowableInformation($throwable);
		}
	}

	
	 public function getFullQualifiedClassname() {
		 return $this->fqcn;
	 }
	 
	
	public function getLocationInformation() {
		if($this->locationInfo === null) {

			$locationInfo = array();

			if(function_exists('debug_backtrace')) {
				$trace = debug_backtrace();
				$prevHop = null;
								$hop = array_pop($trace);
				while($hop !== null) {
					if(isset($hop['class'])) {
												$className = strtolower($hop['class']);
						if(!empty($className) and ($className == 'logger' or $className == 'loggercategory' or 
							strtolower(get_parent_class($className)) == 'logger' or
							strtolower(get_parent_class($className)) == 'loggercategory')) {
							$locationInfo['line'] = $hop['line'];
							$locationInfo['file'] = $hop['file'];
							break;
						}
					}
					$prevHop = $hop;
					$hop = array_pop($trace);
				}
				$locationInfo['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main';
				if(isset($prevHop['function']) and
					$prevHop['function'] !== 'include' and
					$prevHop['function'] !== 'include_once' and
					$prevHop['function'] !== 'require' and
					$prevHop['function'] !== 'require_once') {
	
					$locationInfo['function'] = $prevHop['function'];
				} else {
					$locationInfo['function'] = 'main';
				}
			}
					 
			$this->locationInfo = new LoggerLocationInfo($locationInfo, $this->fqcn);
		}
		return $this->locationInfo;
	}

	
	public function getLevel() {
		return $this->level;
	}

	
	public function getLoggerName() {
		return $this->categoryName;
	}

	
	public function getMessage() {
		if($this->message !== null) {
			return $this->message;
		} else {
			return $this->getRenderedMessage();
		}
	}

	
	public function getNDC() {
		if($this->ndcLookupRequired) {
			$this->ndcLookupRequired = false;
			$this->ndc = LoggerNDC::get();
		}
		return $this->ndc;
	}

	
	public function getMDC($key) {
		return LoggerMDC::get($key);
	}

	
	public function getRenderedMessage() {
		if($this->renderedMessage === null and $this->message !== null) {
			if(is_string($this->message)) {
					$this->renderedMessage = $this->message;
			} else {
																				$repository = Logger::getHierarchy();
				$rendererMap = $repository->getRendererMap();
				$this->renderedMessage= $rendererMap->findAndRender($this->message);
			}
		}
		return $this->renderedMessage;
	}

	
	public static function getStartTime() {
		if(!isset(self::$startTime)) {
			if (function_exists('microtime')) {
								self::$startTime = microtime(true);
			} else {
				self::$startTime = floatval(time());
			}
		}
		return self::$startTime; 
	}

	
	public function getTimeStamp() {
		return $this->timeStamp;
	}
	
	
	public function getTime() {
		$eventTime = (float)$this->getTimeStamp();
		$eventStartTime = (float)LoggerLoggingEvent::getStartTime();
		return number_format(($eventTime - $eventStartTime) * 1000, 0, '', '');
	}
	
	
	public function getThreadName() {
		if ($this->threadName === null) {
			$this->threadName = (string)getmypid();
		}
		return $this->threadName;
	}

	
	public function getThrowableInformation() {
		return $this->throwableInfo;
	}
	
	
	public function toString() {
		serialize($this);
	}
	
	
	public function __sleep() {
		return array(
			'fqcn',
			'categoryName',
			'level',
			'ndc',
			'ndcLookupRequired',
			'message',
			'renderedMessage',
			'threadName',
			'timeStamp',
			'locationInfo',
		);
	}

}

LoggerLoggingEvent::getStartTime();
